dp greedy sortings *2000

Please click on ads to support us..

C++ Code:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF 1e12

const int N = 2e5 + 7;
int n, idx[N];
pair<int, int> a[N];
ll dp[N];

ll rec(int i)
{
    if (i == n)
        return 0;
    if(n - i < 3)
        return INF;
    ll &ret = dp[i];
    if (~ret)
        return ret;
    ret = a[n - 1].first - a[i].first;
    int low = i + 3, high = i + 5;
    for(low; low <= high; ++low)
    {
        ll ans = rec(low), cur = (a[low - 1].first - a[i].first);
        ret = min(ret, ans + cur);
    }
    return ret;
}

void solve()
{
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> a[i].first, a[i].second = i;
    sort(a, a + n);
    memset(dp, -1, sizeof(dp));
    ll ans = rec(0);
    cout << ans << ' ';
    int cnt = 1, lst = 0;
    idx[a[0].second] = cnt;
    // build
    int number = 1;
    for (int i = 1; i < n; ++i)
    {
        if (number > 2 and ans - (a[i - 1].first - a[lst].first) == dp[i])
        {
            cnt++;
            ans -= (a[i - 1].first - a[lst].first);
            lst = i;
            number = 1;
        }
        else
            number++;
        idx[a[i].second] = cnt;
    }
    cout << cnt << '\n';
    for (int i = 0; i < n; ++i)
        cout << idx[i] << ' ';
    cout << '\n';
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int tc = 1;
    // cin >> tc;
    for (int i = 1; i <= tc; ++i)
        solve();
}
				 			     			     	  		 	  	


Comments

Submit
0 Comments
More Questions

1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder